Page({
  data: {
    durationOptions: ['2小时', '4小时', '6小时', '8小时', '全天'],
    selectedDuration: '',
    healthRequirement: '',
    contactName: '',
    phoneNumber: '',
    address: '',
    notes: ''
  },

  onLoad: function() {
    // 页面加载时的初始化逻辑
  },

  // 时长选择
  bindDurationChange(e) {
    const index = e.detail.value;
    this.setData({
      selectedDuration: this.data.durationOptions[index]
    });
  },

  // 健康需求输入
  inputHealthRequirement(e) {
    this.setData({
      healthRequirement: e.detail.value
    });
  },

  // 提交表单
  submitForm() {
    const { selectedDuration, healthRequirement, contactName, phoneNumber, address } = this.data;

    // 验证必填项
    if (!selectedDuration) {
      wx.showToast({
        title: '请选择护理时长',
        icon: 'none'
      });
      return;
    }

    if (!contactName) {
      wx.showToast({
        title: '请输入联系人姓名',
        icon: 'none'
      });
      return;
    }

    if (!phoneNumber) {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none'
      });
      return;
    } else if (!/^1[3-9]\d{9}$/.test(phoneNumber)) {
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none'
      });
      return;
    }

    if (!address) {
      wx.showToast({
        title: '请输入家庭住址',
        icon: 'none'
      });
      return;
    }

    // 构建预约信息
    const appointmentInfo = {
      duration: selectedDuration,
      healthRequirement: healthRequirement || '无特殊需求',
      contactName: contactName,
      phoneNumber: phoneNumber,
      address: address,
      notes: notes || '无特殊要求'
    };

    // 这里可以添加提交到服务器的逻辑
    wx.showModal({
      title: '预约成功',
      content: `您已成功预约陪伴护理服务，\n时长：${appointmentInfo.duration}\n健康需求：${appointmentInfo.healthRequirement}\n联系人：${appointmentInfo.contactName}\n手机号码：${appointmentInfo.phoneNumber}\n家庭住址：${appointmentInfo.address}\n备注：${appointmentInfo.notes}`,
      showCancel: false,
      success: (res) => {
        if (res.confirm) {
          wx.navigateBack();
        }
      }
    });
  },

  // 联系人姓名输入
  inputContactName(e) {
    this.setData({
      contactName: e.detail.value
    });
  },

  // 手机号码输入
  inputPhoneNumber(e) {
    this.setData({
      phoneNumber: e.detail.value
    });
  },

  // 家庭住址输入
  inputAddress(e) {
    this.setData({
      address: e.detail.value
    });
  },

  // 备注要求输入
  inputNotes(e) {
    this.setData({
      notes: e.detail.value
    });
  },

  // 返回上一页
  navigateBack() {
    wx.navigateBack();
  }
});